package com.NeuglsWorkStudio.UI.FormElement.Component.Video
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.Colors.RGB;
	import com.NeuglsWorkStudio.UI.Buttons.TTipButton;
	import com.NeuglsWorkStudio.UI.FormElement.Component.TComponent;
	import com.NeuglsWorkStudio.UI.FormElement.Component.TLabel;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TLabelConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TVideoControlBarConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Interfaces.IMediaControl;
	import com.NeuglsWorkStudio.UI.FormElement.Interfaces.MediaStatusType;
	import com.NeuglsWorkStudio.UI.GradientBar;
	import com.NeuglsWorkStudio.Utility.Utility;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.utils.Timer;
	
	
	/**实现一个播放器控制条
	 * @author Neugls 2011/8/5**/
	public class TVideoControlBar extends TComponent
	{
		[Embed(source='images/Button/SoundOn.png')]
		private var SoundImg:Class;
		
		[Embed(source='images/Button/SoundOff.png')]
		private var SoundOffImg:Class;
		
		[Embed(source='images/Button/FullScreen.png')]
		private var FullScreenImg:Class;
		
		[Embed(source='images/Button/Play.png')]
		private var PlayImg:Class;
		
		[Embed(source='images/Button/pause.png')]
		private var PauseImg:Class;
		
		private var _FMediaControlInterface:IMediaControl;
		private var _FPlayBtn:TTipButton;
		private var _FSlider:Sprite;
		private var _FSliderBar:GradientBar;
		private var _FTimeLabel:TLabel;
		private var _FSoundBtn:TTipButton;
		private var _FFullScreenBtn:TTipButton;
		private var _FCanUpdateProgress:Boolean=true;
		private var _FSliderMouseDown:Boolean=false;
		private var _FUpdateProgressTimer:Timer;
		
		
		public static var PlayButtonHeight:Number=40;
		
		public function TVideoControlBar(AConfig:TConfig)
		{
			super(AConfig);
		}

		public function get MediaControlInterface():IMediaControl
		{
			return _FMediaControlInterface;
		}
		
		public function VideoControlBarConfig():TVideoControlBarConfig{
			return _FConfig as TVideoControlBarConfig;
		}
		
		override protected function Init():void{
			super.Init();
			VideoControlBarConfig().BaseColor
			
			_FMediaControlInterface=VideoControlBarConfig().MediaControlInterface;
			
			_FPlayBtn=new TTipButton('',false);
			_FPlayBtn.Image=new PlayImg() as Bitmap;
			_FPlayBtn.Tip='';
			_FPlayBtn.Image2=new PauseImg() as Bitmap;
			_FPlayBtn.DoubleStateButton=true;
			_FPlayBtn.OnClick=function():void{
				if(_FMediaControlInterface.PlayStatus==MediaStatusType.UnInit){
					_FMediaControlInterface.Play();					
				}else{
					_FMediaControlInterface.Toggle();
				}
			}
			_FPlayBtn.OnClick2=_FPlayBtn.OnClick;
			
			this.addChild(_FPlayBtn);
			
			_FSliderBar=CreatePlaySliderBar();
			_FSliderBar.buttonMode=true;
			_FSliderBar.addEventListener(MouseEvent.CLICK,ClickSeek);
			this.addChild(_FSliderBar);
			
			
			_FSlider=CreatePlaySlider();
			this.addChild(_FSlider);
			_FSlider.buttonMode=true;
			
			var labelConfig:TLabelConfig=new TLabelConfig();
			labelConfig.Color=VideoControlBarConfig().BaseColor;
			labelConfig.Font='Tahoma';
			labelConfig.Name='timelabel';
			labelConfig.Size=12;
			labelConfig.Caption='00:00';
			_FTimeLabel=new TLabel(labelConfig);
			
			this.addChild(_FTimeLabel);
			
			_FSoundBtn=new TTipButton('',false);
			_FSoundBtn.Image=new SoundImg() as Bitmap;
			_FSoundBtn.Image2=new SoundOffImg() as Bitmap;
			_FSoundBtn.OnClick=SetSound;
			_FSoundBtn.OnClick2=SetSound;
			_FSoundBtn.DoubleStateButton=true;
			//_FSoundBtn.DoubleStateState=true;
			
			this.addChild(_FSoundBtn);
			
			_FFullScreenBtn=new TTipButton('',false);
			_FFullScreenBtn.Image=new FullScreenImg() as Bitmap;
			_FFullScreenBtn.OnClick=VideoControlBarConfig().OnFullScreen;
			
			
			this.addChild(_FFullScreenBtn);
			
			
			Paint();
			
			_FSlider.addEventListener(MouseEvent.MOUSE_DOWN,function(E:MouseEvent):void{
				if(_FMediaControlInterface.PlayStatus==MediaStatusType.UnInit)
					return;
				_FCanUpdateProgress=false;
				_FSliderMouseDown=true;
				_FSlider.startDrag(false,new Rectangle(_FSliderBar.x,_FSlider.y,_FSliderBar.Width,0));
			});
			
			this.addEventListener(MouseEvent.MOUSE_UP,function(E:MouseEvent):void{
				if(!_FSliderMouseDown) return;
				_FSliderMouseDown=false
				_FSlider.stopDrag();
				_FMediaControlInterface.Seek((_FSlider.x-_FSliderBar.x)/_FSliderBar.Width*_FMediaControlInterface.Duration);
				_FCanUpdateProgress=true;
			});
			
			this.addEventListener(Event.ADDED_TO_STAGE,function(E:Event):void{
				ToAddLisener();
			},false,0,true);
			this.addEventListener(Event.REMOVED_FROM_STAGE,ToStop,false,0,true);
			
			this._FMediaControlInterface.OnPause=function():void{
				_FPlayBtn.DoubleStateState=false;
			}
			
			this._FMediaControlInterface.OnPlay=function():void{
				_FPlayBtn.DoubleStateState=true;
			}
		}
		
		private function ClickSeek(Event:MouseEvent):void{
			
			_FMediaControlInterface.Seek(
				Event.localX/_FSliderBar.Width*_FMediaControlInterface.Duration);
		}
		private function ToStop(E:Event):void{
			if(!_FUpdateProgressTimer) return;
			_FUpdateProgressTimer.stop();
			_FUpdateProgressTimer=null;
		}
		
		private function ToAddLisener():void{
			if(this._FMediaControlInterface.PlayStatus!=MediaStatusType.UnInit){
				this.UpdateInfo();
			}
		}
		
		//创建一个Time来实时更新播放进度
		private function ToUpdateTimeAndProgress():void{
			if(!_FUpdateProgressTimer){
				_FUpdateProgressTimer=new Timer(500);
				_FUpdateProgressTimer.addEventListener(TimerEvent.TIMER,UpdateTimeAndProgress);
			}
			_FUpdateProgressTimer.start();
		}
		
		private function UpdateTimeAndProgress(E:Event):void{
			_FTimeLabel.Caption=FormatTime(_FMediaControlInterface.CurrentPos)+'/'+FormatTime(_FMediaControlInterface.Duration);
			if(_FCanUpdateProgress)
				_FSlider.x=_FSliderBar.x+
				(_FMediaControlInterface.CurrentPos/_FMediaControlInterface.Duration)*_FSliderBar.Width;
		}
		
		
		public function UpdateInfo():void{
			this.ToUpdateTimeAndProgress();
		}
		
		public function StopInfo():void{
			this.ToStop(null);
		}
		
		protected function Paint():void{
			
			_FSliderBar.Width=VideoControlBarConfig().Width-6;
			_FSliderBar.Resize();
			_FSliderBar.x=3;
			_FSliderBar.y=0;
			_FSlider.x=_FSliderBar.x+_FSlider.width/2;
			_FSlider.y=_FSliderBar.y+_FSliderBar.Height/2;
			
			this._FPlayBtn.x=_FPlayBtn.width/2+3;
			_FPlayBtn.y=_FSliderBar.y+_FSliderBar.Height+2+_FPlayBtn.height/2;
			
			_FTimeLabel.x=_FPlayBtn.x+_FPlayBtn.width/2+5;
			_FTimeLabel.y=_FSliderBar.y+_FSliderBar.Height+2;
			
			_FFullScreenBtn.x=this.width-_FFullScreenBtn.width-5;
			_FFullScreenBtn.y=_FSliderBar.y+_FSliderBar.Height+2+_FFullScreenBtn.height/2;
			
			_FSoundBtn.x=_FFullScreenBtn.x-_FSoundBtn.width-5;
			_FSoundBtn.x=this.width-_FSoundBtn.width-5;
			_FSoundBtn.y=_FSliderBar.y+_FSliderBar.Height+2+_FSoundBtn.height/2;
			
			with(this.graphics){
				clear();
				//beginFill(ColorUtility.GetBackColorFromBaseColor(VideoControlBarConfig().BaseColor),1);
				beginFill(0x000000,1);
				drawRect(0,0,VideoControlBarConfig().Width,this.height);
				endFill();
			}
		}
		
		private function SetSound():void{
			_FMediaControlInterface.Volume^=1
		}
		
		/**注册点在中间**/
		private function CreatePlaySlider():Sprite{
			var Result:Sprite=new Sprite();
			var R:Number=8;
			var r:Number=6;
			with(Result.graphics){
				beginFill(VideoControlBarConfig().BaseColor,1);
				drawCircle(0,0,r);
				lineStyle(1,ColorUtility.GetBorderColorFromBaseColor(VideoControlBarConfig().BaseColor));
				//beginFill(VideoControlBarConfig().BaseColor,0);
				//drawCircle(0,0,R);
				endFill();
			}
			return Result;
		}
		
		private function CreatePlaySliderBar():GradientBar{
			var H:Number=4;
			var W:Number=VideoControlBarConfig().Width-_FPlayBtn.width-5;
			var Result:GradientBar=new GradientBar(VideoControlBarConfig().BaseColor,W,H);
			return Result;
		}
						
		
		private function GetPlayBtnStatus(MouseOver:Boolean):Sprite{
			var r:Number=PlayButtonHeight/2;
			var Result:Sprite=new Sprite();
			with(Result.graphics){
				lineStyle(1,ColorUtility.GetBorderColorFromBaseColor(VideoControlBarConfig().BaseColor));
				drawCircle(r,r,r);
				endFill();
			}
			
			var Color:int=MouseOver?RGB.AddToRGBColor(VideoControlBarConfig().BaseColor,-6):VideoControlBarConfig().BaseColor;
			var T:Sprite=Utility.CreateTriangle(Utility.tdRight,r*2-5,Color);
			with(Result.addChild(T)){
				x=r;
				y=r;
			}
			
			return Result;
		}
		
		private function FormatTime(Time:Number):String //时间（秒）转化格式为00：00
		{
			return ("0"+uint(Time/60)).substr(-2)+":"+("0"+int(Time%60)).substr(-2);
		}
		
		public function Resize(W:Number):void{
			VideoControlBarConfig().Width=W;
			Paint();
		}
	}
}